<?php

class Dao extends Eloquent{

    protected $table;
    public $incrementing=true;
    protected $query;
    protected $fillable;
    
    function __construct() {
        parent::__construct();
        $this->query = $this;
    }
    
    public function insertObject($obj) {
        try {
            if(!$obj->getId()==null) {
                parent::insert($obj->toArray());
                return $obj->getId();
            }
            return parent::insertGetId($obj->toArray());
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
	
    public function updateObject($obj, $old_id) {
        try{
            return parent::where('id',$old_id)->update($obj->toArray());
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
//	
//	public function updateWhere($obj, $arr_where) {
//		$this->db->update($this->mTableName, $obj->toArray(), $arr_where);
//		return true;
//	}
//	
    public function deleteObject($id) {
        try{
            return parent::where('id',$id)->delete();
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
    
//	public function deleteWhere($arr_where) {
//		$this->db->delete($this->mTableName, $arr_where);
//		return $this->db->affected_rows();
//	}

    public function getObject($id) {
        try{
            $model = parent::find($id);
            if(!is_null($model)) return $model->toObject();
            else return null;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }

    
    public function getList($criteria=null) {
        try{
            $queryResult =  parent::all();
            $results = array();
            foreach($queryResult as $obj){
                $results[] = $obj->toObject();
            }
            return $results;

        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
    
    public function getListCount($criteria=null) {
        try{
            return parent::count();
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
	
    public function beginTrans() {
        return DB::connection()->getPdo()->beginTransaction();
    }
    
    public function commitTrans() {
        return DB::connection()->getPdo()->commit();
    }

    public function roolback() {
        return DB::connection()->getPdo()->rollBack();
    }
    
    protected function toObject($rowdata=null) {
        throw new Exception('There is no implementation of toObject() method in current dao instance!'); 
    }
}
